package cn.wenhe9.gulimall.product.controller;

import java.util.Arrays;
import java.util.Map;

import cn.wenhe9.common.utils.ResultResponse;
import cn.wenhe9.gulimall.product.entity.AttrGroupEntity;
import cn.wenhe9.gulimall.product.vo.AttrVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.wenhe9.gulimall.product.entity.AttrEntity;
import cn.wenhe9.gulimall.product.service.AttrService;



/**
 * 商品属性
 *
 * @author dujinliang
 * @email dujinliang9@163.com
 * @date 2022-04-14 19:17:08
 */
@RestController
@RequestMapping("/api/product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    /**
     * 列表
     */
    @RequestMapping("/base/list/{catId}")
    //@RequiresPermissions("product:attr:list")
    public ResultResponse list(
            @PathVariable(name = "catId") Long catId,
            @RequestParam(defaultValue = "0") Long page,
            @RequestParam(defaultValue = "10") Long limit,
            String key
    ){
        if (catId == 0){
            Page<AttrEntity> groupPage = attrService.lambdaQuery().like(AttrEntity::getAttrName, key).page(new Page<>(page, limit));
            return ResultResponse.ok(groupPage);
        }else {
            Page<AttrEntity> groupPage = attrService.lambdaQuery().eq(AttrEntity::getCatelogId, catId).like(AttrEntity::getAttrName, key).page(new Page<>(page, limit));
            return ResultResponse.ok(groupPage);
        }
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public ResultResponse info(@PathVariable("attrId") Long attrId){
		AttrEntity attr = attrService.getById(attrId);

        return ResultResponse.ok(attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public ResultResponse save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return ResultResponse.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public ResultResponse update(@RequestBody AttrEntity attr){
		attrService.updateById(attr);

        return ResultResponse.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public ResultResponse delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return ResultResponse.ok();
    }

}
